package com.oa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oa.mapper.DeptMapper;
import com.oa.mapper.RoleMapper;
import com.oa.mapper.UserMapper;
import com.oa.pojo.Option;
import com.oa.pojo.PageBean;
import com.oa.pojo.PageInfo;
import com.oa.pojo.User;
import com.oa.service.UserService;
import com.oa.utils.EntityException;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author Str
 * @Date 2021/12/6 19:24
 * @Description :
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<User> getAll() {

        return userMapper.getAll();
    }

    @Override
    public User getOne(@Param("userId") String userId) {
        return userMapper.getOne(userId);
    }

    @Override
    public String getDeptID(@Param("userId") String userId) {
        return userMapper.getDeptID(userId);
    }

    @Override
    public String getRoleId(String userId) {
        return userMapper.getRoleId(userId);
    }

    @Override
    public User getLeaderByID(@Param("userId") String userId) {
        return userMapper.getLeaderByID(userId);
    }

    @Override
    public User getOneByName(String username) {
        return userMapper.getOneByName(username);
    }

    @Override      //下拉列表{1}
    public List<Option> getDeptOptions() {
        return userMapper.queryDeptOptions();
    }

    @Override      //下拉列表{2}
    public List<Option> getRoleOptions() {
        return userMapper.queryRoleOptions();
    }


//    @Override
//    public PageBean getUserList(int pageNum, int pageSize, Map data) {
//        PageHelper.startPage(pageNum, pageSize);
//        //{1}计算偏移记录数。
//        int offset = (pageNum - 1) * pageSize;
//        data.put("offset", offset);
//        data.put("limit", pageSize);
//        //{2}你们自己使用分页插件, 实现分页。
//        List<User> list = null;
//        // 用于模糊搜索
////        for (Object o : data.keySet()) {
////            data.put(o,"%"+data.get(o)+"%");
////        }
//        System.out.println("UserServiceImpl.getUserList.data================" + data);
//        //{3}data 存的是关键字数据
//        list = userMapper.queryList(data);
//        System.out.println("UserServiceImpl.getUserList.list================" + list);
//        //{ps}你也可以使用分页插件的 Page 对象。
//        PageBean pBean = new PageBean();
//        //这是临时写法 {以后要改}
//        pBean.setCount(list.size());
//        pBean.setLimit(pageSize);
//        pBean.setPage(list.size());
//        pBean.setList(list);
//        System.out.println("pBean=============");
//        System.out.println(pBean);
//        return pBean;
//    }

    @Override
    public Page<User> getUserList(int page, int limit, Map data) {
        // 开启分页: true 获取记录数...
        Page<User> pages = PageHelper.startPage(page, limit, true);
        // PageHelper会拦截查找内容
        userMapper.queryList(data);
        return pages;
    }

    @Override
    public void saveUser(Map<String, String> map) throws EntityException {
        User user = new User();
        user.setId(map.get("id"));
        user.setUsername(map.get("username"));
        user.setPassword(map.get("password"));
        user.setNickName(map.get("nickName"));
        user.setSex(map.get("sex"));
        user.setDeptId(map.get("deptId"));
        user.setEmail(map.get("email"));
        user.setNo(map.get("no"));

        String comment = map.get("username") + "[" + roleMapper.getRoleNameByRoleId(map.get("roleId"))
                + "][" + map.get("deptId") + ":" + deptMapper.getDeptName(map.get("deptId")) + "]";

        //{ps}这里要同时处理两件事情 [新增 / 更新]
        try {
            //{3}如果 userID 为空 ----> 新增用户
            if (map.get("id") == null) {
                //{a}查询用户是否存在
                User find = userMapper.getOneByName(map.get("username"));
                //{b}如果用户存在, 不能新增用户
                if (find != null) {
                    throw new EntityException("用户名已存在。");
                }
                //{b}创建一个 uuid [A87E....]
                String userId = UUID.randomUUID().toString();
                //(原)"A87E-C5E6-.."  ----> (新)"A87EC5E6.."
                userId = userId.replace("-", "");


                //{b}设置给 user
                user.setId(userId);

                System.out.println("UserService.user::" + user);
                //{c}新增用户
                userMapper.addUser(user);
                // user_role添加数据
                System.out.println("UserService.comment::" + comment);
                userMapper.addUserRole(userId, map.get("roleId"), comment);
            } else {    //不为空 ---> 更新用户
                user.setCreateDate(map.get("createDate"));
                System.out.println("updateUser:" + user);
                userMapper.updateUser(user);
                userMapper.updateUserRole(user.getId(), map.get("roleId"), comment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int delUser(String userId) throws EntityException {
        int n = userMapper.delUser(userId);
        int m = userMapper.delUserRole(userId);
        if ((m + n) != 2) {
            throw new EntityException("删除失败");
        }
        return (m + n);
    }

    // 根据用户名和密码登录
    @Override
    public User login(@Param("username") String username, @Param("password") String password){
        User user = userMapper.login(username, password);
        return user;
    }
}
